//
//  LocateViewController.m
//  CYWFrame
//
//  Created by 于顺燊 on 2018/10/16.
//  Copyright © 2018年 Macx. All rights reserved.
//

#import "LocateViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapLocationKit/AMapLocationKit.h>

@interface LocateViewController ()<UISearchBarDelegate, MAMapViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *address;
@property (weak, nonatomic) IBOutlet UILabel *latitude;
@property (weak, nonatomic) IBOutlet UILabel *longtitude;
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (strong, nonatomic) MAMapView *mapView;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;

@end

@implementation LocateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
	[self createUI];
}

- (void)createUI {
	self.view.backgroundColor = UISeparatedColorRGB;
	UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(100, 0, SCREEN_WIDTH-130, 30)];
	searchBar.placeholder = @"请输入要查询的商家…";
	searchBar.backgroundImage = IMAGE(@"clearImage");
	searchBar.layer.cornerRadius = searchBar.height/2;
	searchBar.layer.masksToBounds = YES;
	searchBar.keyboardType = UIKeyboardTypeDefault;
	searchBar.delegate = self;
	searchBar.searchBarStyle = UISearchBarStyleDefault;
	
	UIView *searchTextField = [searchBar valueForKey:@"_searchField"];
	searchTextField.backgroundColor = [UIColor colorWithRed:234/255.0 green:235/255.0 blue:237/255.0 alpha:1];
	self.navigationItem.titleView = searchBar;
	[AMapServices sharedServices].enableHTTPS = YES;
	_mapView = [[MAMapView alloc] initWithFrame:self.topView.bounds];
	_mapView.showsUserLocation = YES;
	_mapView.delegate = self;
	_mapView.userTrackingMode = MAUserTrackingModeFollow;
	[self.topView addSubview:_mapView];

	self.confirmBtn.layer.cornerRadius = 10.0;

}
- (IBAction)confirmBtnClick:(id)sender {
	
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
	[searchBar resignFirstResponder];
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
	return YES;
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
	NSLog(@"开始输入搜索内容");
	
	// 取消字体颜色？
	searchBar.showsCancelButton = YES;
	[searchBar setShowsCancelButton:YES animated:YES];
	UITextField *searchBarTextField = nil;
	NSArray *views = ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) ? searchBar.subviews : [[searchBar.subviews objectAtIndex:0] subviews];
	for (UIView *subview in views)
	{
		if ([subview isKindOfClass:[UITextField class]])
		{
			searchBarTextField = (UITextField *)subview;
			break;
			
		}
	}
	searchBarTextField.enablesReturnKeyAutomatically = NO;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
	NSLog(@"输入搜索完毕");
	
	[searchBar setShowsCancelButton:NO animated:YES];
	[searchBar resignFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
	NSLog(@"取消搜索");
	
	[searchBar setShowsCancelButton:NO animated:YES];
	[searchBar resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
